package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.Scenic;
import cn.wolfcode.luowowo.article.domain.ScenicDetail;
import cn.wolfcode.luowowo.article.query.ScenicQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IScenicDetailService;
import cn.wolfcode.luowowo.article.service.IScenicService;
import cn.wolfcode.luowowo.cache.service.IScenicStatisVORedisService;
import cn.wolfcode.luowowo.cache.vo.ScenicStatisVO;
import cn.wolfcode.luowowo.comment.domain.ScenicComment;
import cn.wolfcode.luowowo.comment.query.ScenicCommentQuery;
import cn.wolfcode.luowowo.comment.service.IScenicCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.web.annotation.RequireLogin;
import cn.wolfcode.luowowo.website.web.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("scenic")
public class ScenicController {
    @Reference
    private IScenicService scenicService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IScenicDetailService scenicDetailService;
    @Reference
    private IScenicStatisVORedisService scenicStatisVORedisService;
    @Reference
    private IScenicCommentService scenicCommentService;



    @RequestMapping("list")
    public String list(Model model, @ModelAttribute("qo")ScenicQuery qo){
        Long destId = qo.getDestId();
        List<Scenic> scenicsTop5 = scenicService.selectTop5(destId);
        // 共享页面上的景点概况
        List<Destination> toasts = destinationService.getToasts(destId);
        Destination destination = toasts.get(toasts.size() - 1);
        model.addAttribute("info", destination.getInfo());
        model.addAttribute("dest", destination);
        // 查询必去的五个景点  排序根据评论数
        model.addAttribute("Top5", scenicsTop5);

        // 查询热门景点
        List<Scenic> travelHotPointTop10 = scenicService.selectHotScenicTop10(destId);
        model.addAttribute("top10", travelHotPointTop10);
        // 查询该目的下的全部景点
        qo.setPageSize(10);
        PageInfo pageInfo = scenicService.query(qo);

        model.addAttribute("pageInfo", pageInfo);
        return "scenic/list";
    }

    // 查询景点明细
    @RequestMapping("/detail")
    public String detail(Model model, Long id) {
        ScenicDetail scenicDetail = scenicDetailService.selectByPrimaryKey(id);
        model.addAttribute("detail", scenicDetail);
        // 共享封面url
        Scenic scenic = scenicService.selectByPrimaryKey(id);
        model.addAttribute("scenic", scenic);

        Long destId = scenic.getDestId().longValue();
        List<Scenic> selectTop5 = scenicService.selectTop5(destId);
        // 共享页面上的景点概况
        List<Destination> toasts = destinationService.getToasts(destId);
        Destination destination = toasts.get(toasts.size() - 1);
        model.addAttribute("dest", destination);

        // 查询所有的评论

        ScenicCommentQuery qo = new ScenicCommentQuery();
        qo.setScenicId(id);
        qo.setPageSize(Integer.MAX_VALUE);
        Page page = scenicCommentService.query(qo);
        model.addAttribute("list", page.getContent());
        ScenicStatisVO scenicStatisVO = scenicStatisVORedisService.getScenicStatisVO(id);
        model.addAttribute("vo", scenicStatisVO);
        return "scenic/detail";
    }


    // 收藏
    // 攻略收藏   tpid  景点的id
    @RequestMapping("/favor")
    @ResponseBody
    public Object favor(Long tpid, @UserParam UserInfo userInfo) {
        if (userInfo == null) {
            AjaxResult result = new AjaxResult(false, "请先登录");
            result.setCode(AjaxResult.RESPONSE_CODE_NOLOGIN);
            return result;
        }
        // true表示收藏, false 表示取消收藏
        boolean ret = scenicStatisVORedisService.favor(userInfo.getId(), tpid);
        return new AjaxResult(ret, null).addData(scenicStatisVORedisService.getScenicStatisVO(tpid));
    }



    @RequestMapping("/hasGone")
    @ResponseBody
    public Object hasGone(Long tpid, @UserParam UserInfo userInfo) {
        if (userInfo == null) {
            AjaxResult result = new AjaxResult(false, "请先登录");
            result.setCode(AjaxResult.RESPONSE_CODE_NOLOGIN);
            return result;
        }
        // true表示没去过, false 表示取消收藏
        boolean ret = scenicStatisVORedisService.hasGone(userInfo.getId(), tpid);

        if (ret == false) {
            return new AjaxResult(ret, "去过了就没法再点了,谢谢");
        } else {

            return new AjaxResult(ret, null).addData(scenicStatisVORedisService.getScenicStatisVO(tpid));
        }
    }


    @RequestMapping("/commentAdd")
    @RequireLogin
    public Object commentAdd(ScenicComment comment, @UserParam UserInfo userInfo, Model model) {
        comment.setUserId(userInfo.getId());
        comment.setHeadUrl(userInfo.getHeadImgUrl());
        comment.setLevel(userInfo.getLevel());
        comment.setUsername(userInfo.getNickname());
        scenicCommentService.save(comment);

        // 评论数+1
        scenicStatisVORedisService.replynumIncrease(comment.getScenicId(), 1);
        ScenicStatisVO scenicStatisVO = scenicStatisVORedisService.getScenicStatisVO(comment.getScenicId());
        model.addAttribute("vo", scenicStatisVO);

        // 获取景点评论数   从vo对象中
        return "redirect:/scenic/detail?id=" + comment.getScenicId();
        // model.addAttribute("c",comment1);
//        return "scenic/commentTpl";
    }


    // 回复
    @RequestMapping("/commentReply")
    @RequireLogin
    @ResponseBody
    public Object commentReply(ScenicComment comment, @UserParam UserInfo userInfo, Model model) {
        comment.setUserId(userInfo.getId());
        comment.setHeadUrl(userInfo.getHeadImgUrl());
        comment.setUsername(userInfo.getNickname());
        ScenicComment commentOld = scenicCommentService.findById(comment.getId());
        List<ScenicComment> comments = new ArrayList();
        List<ScenicComment> refComments = commentOld.getRefComment();
        if(refComments != null && refComments.size() >= 0){
            for (ScenicComment refComment : refComments) {
                comments.add(refComment);
            }
        }
        comments.add(comment);
        commentOld.setId(comment.getId());
        commentOld.setRefComment(comments);
        ScenicComment comment1 = scenicCommentService.save(commentOld);
        //评论数+1
        scenicStatisVORedisService.replynumIncrease(comment1.getScenicId(), 1);

        return AjaxResult.SUCCESS;
    }


    // 评论列表
    @RequestMapping("/comment")
    public String commentList(Model model, @ModelAttribute("qo") ScenicCommentQuery qo) {

        qo.setPageSize(Integer.MAX_VALUE);

        Page page = scenicCommentService.query(qo);
        model.addAttribute("page", page);
        model.addAttribute("c",scenicCommentService.query(qo).getContent());
        return "scenic/commentTpl";
    }
}
